﻿/** 
 * Simplest Librtmp Send 264 
 * liuxibo
 */  
  
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <stdint.h>
#include <math.h>

#include <algorithm>
#include <unistd.h>
#include <iostream>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std;
using namespace cv;

//test fast detector
void fast_detector(Mat& img ,int t){

    vector<KeyPoint> keypoints;    //KeyPoint是opencv中定义的特征点类
    FastFeatureDetector fast(t);  //定义FAST特征检测器，这里的40是算法中的阈值t
    fast.detect(img, keypoints);   //检测img图像，特征点存在keypoints中
    printf("keypoints:%ld\n",keypoints.size() );
    for (size_t i = 0; i < keypoints.size(); i++)
    {
        circle(img, Point(keypoints[i].pt.x, keypoints[i].pt.y), 2, Scalar(0, 255, 0), 1);
    }
    
    //imshow("photo.jpg", img);
    //char file_name[32];  
    //sprintf(file_name, "result_%d_%s",t, file_path); //(const char *)msg, sizeof(msg)
    //imwrite(file_name,img);
    //printf("detector_photo write to file.\n" );
}


//test camera's fps
void show_fps(bool show){
    Mat frame;  
    VideoCapture capture(0);
    //VideoCapture capture("GOPR1802.MP4");  
    //VideoCapture capture("20161014_152911.mp4");
    //capture.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
    //capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);  
    //capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480); 
    
    long totalFrameNumber = 0;
    double rate = 120;
    int delay = 1000/rate;
    //for local video
    if(0)
    {
        //http://blog.csdn.net/thefutureisour/article/details/7530344
        //get count of frame
        totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);  
        cout<<"total:"<<totalFrameNumber<<"frame"<<endl;  
     
        //gei fps
        rate = capture.get(CV_CAP_PROP_FPS);  
        cout<<"fps:"<<rate<<endl;  

        delay = 1000/rate;  
    }
    

    double fps;  
    char str[5];  
    if(show){
        namedWindow("webCam");
    }
    
    double t = 0;  
    long i = 0;
    while (1)  
    {  
        t = (double)getTickCount();  
        if (waitKey(delay) == 27)  
        {
                break;  
        }  
        if (capture.isOpened())  
        {
            //读取下一帧  
            capture >> frame;  
            // do something ...  
            int tag = 30;
            i++;

            fast_detector(frame, tag);
            
            char file_name[32];  
            sprintf(file_name, "./result/result_%d_%ld.jpg",tag,i); //(const char *)msg, sizeof(msg)
            //imwrite(file_name,frame);


            t = ((double)getTickCount() - t) / getTickFrequency();  
            fps = 1.0 / t;  
            sprintf(str, "%5f", fps);  
            string fpsStr("FPS:");  
            fpsStr += str;  
            printf("fps:%s\n",str);

            putText(frame, fpsStr, Point(0,30), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0,0,255));  
            
            if(show){
                imshow("webCam", frame);
            }
        }  
        else  
        {  
            std::cout << "capture device failed to open!" << std::endl;  
            break;  
        }  

        //int c = waitKey(rate);  
    }  
}


//main
int main(int argc,char*argv[])
{
    //./fast_test svo_sample.jpg 40
    //Mat img = imread(argv[1]);
    //fast_detector(img, std::atoi( argv[2] ));
    show_fps(false);
    waitKey(0);
    return 0;
}
